package com.meirengu.webview.controller;

import com.meirengu.channel.service.HouBankService;
import com.meirengu.channel.service.PACAService;
import com.meirengu.channel.service.PingAnService;
import com.meirengu.channel.service.YiCaiDaoService;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.webview.model.KwInterfaceRetry;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * ${DESCRIPTION}
 *
 * @author Mao
 * @create 2017-12-19 12:27
 */
@Controller
@RequestMapping("handRetry")
public class HandRetryController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    UserService userService;
    @Autowired
    PingAnService pingAnService;
    @Autowired
    KwUserChannelService kwUserChannelService;
    @Autowired
    private KwDfrzFilterService kwDfrzFilterService;
    @Autowired
    KwThirdbackService kwThirdbackService;
    @Autowired
    KwInterfaceRetryService kwInterfaceRetryService;
    @Autowired
    PACAService pacaService;
    @Autowired
    private KwDfrzPushService kwDfrzPushService;
    @Autowired
    HouBankService houBankService;
    @Autowired
    YiCaiDaoService yiCaiDaoService;
    @Autowired
    KwOperationLogService kwOperationLogService;
    @Autowired
    private KwXbbFilterService kwXbbFilterService;
    @Autowired
    private KwYcdFilterService kwYcdFilterService;
    @Autowired
    private KwXxjrFilterService kwXxjrFilterService;
    @Autowired
    private KwXxjrPushService kwXxjrPushService;
    @Autowired
    private KwPpdPushLogService kwPpdPushLogService;
    @Autowired
    private KwPpdPushService kwPpdPushService;
    @Autowired
    private KwPpdFilterService kwPpdFilterService;
    @Autowired
    private KwZhudaiPushService kwZhudaiPushService;
    @Autowired
    private KwZhudaiFilterService kwZhudaiFilterService;

    Logger logger = LoggerFactory.getLogger(HandRetryController.class);


    /**
     * 补发数据
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @throws Exception
     */
    @RequestMapping("/handRetryInterface")
    public void handRetryInterface(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                                   @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                                   @RequestParam(value = "message", required = false) String message,
                                   @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            Date date = DateUtils.strToDate(createTimeBegin);
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            Date date = DateUtils.strToDate(createTimeEnd);
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryInterface",
                "补发春雨普惠数据", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                null, message, null);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    userInfoService.sendPuHui(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("IP限制");
                    kwInterfaceRetry.setInterfaceCode("pinganph");
                    kwInterfaceRetry.setInterfaceName("平安普惠");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(thirdbackMap.get("createDate").toString()));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }

            }
        }


    }


    /**
     * 补发平安赠险
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryPinganZX")
    public void handRetryPinganZX(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                                  @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                                  @RequestParam(value = "code", required = false) Integer code,
                                  @RequestParam(value = "message", required = false) String message,
                                  @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryPinganZX",
                "补发平安赠险", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    userInfoService.sendPingAnZX(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("IP限制");
                    kwInterfaceRetry.setInterfaceCode("pinganzx");
                    kwInterfaceRetry.setInterfaceName("平安赠险");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(thirdbackMap.get("createDate").toString()));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }
            }
        }
    }

    /**
     * 补发中腾信
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryZTX")
    public void handRetryPinganZTX(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                                   @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                                   @RequestParam(value = "code", required = false) Integer code,
                                   @RequestParam(value = "message", required = false) String message,
                                   @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }

        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryZTX",
                "补发中腾信", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    kwYcdFilterService.sendZTX(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("去除企业主限制，重新发送");
                    kwInterfaceRetry.setInterfaceCode("ztx");
                    kwInterfaceRetry.setInterfaceName("中腾信");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(thirdbackMap.get("createDate").toString()));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }
            }
        }
    }


    /**
     * 补发拍拍贷沉睡接口
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryPpdsu")
    public void handRetryPpdsu(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                               @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                               @RequestParam(value = "httpCode", required = false) Integer httpCode,
                               @RequestParam(value = "httpContent", required = false) String httpContent,
                               @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (httpCode != null) {
            map.put("httpCode", httpCode);
        }
        if (StringUtils.isNotBlank(httpContent)) {
            map.put("httpContent", httpContent);
        }

        List<Map<String, Object>> errorPushList = kwPpdPushLogService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryPpdsu",
                "补发拍拍贷沉睡用户", createTimeBegin, createTimeEnd, "kw_ppd_push_log", errorPushList.size(),
                httpCode + "", httpContent, interfaceCode);
        if (errorPushList != null && errorPushList.size() > 0) {
            for (Map<String, Object> pushLogMap : errorPushList) {
                String mobile = pushLogMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有拍拍贷老用户发送成功的记录
                    Map<String, Object> ppdsuparams = new HashMap<>();
                    ppdsuparams.put("mobile", mobile);
                    ppdsuparams.put("interfaceCode", "ppdsu");
                    ppdsuparams.put("code", "1");
                    Integer ppdsuPushCount = kwPpdPushService.getCount(ppdsuparams);
                    //已有成功发送记录
                    if (!NumberUtil.isNullOrZero(ppdsuPushCount)) {
                        continue;
                    }
                    kwPpdFilterService.sendPpdaiBySleepingUser(userInfo, user);
                }
            }
        }
    }


    /**
     * 补发东方融资网接口
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryDfrz")
    public void handRetryDfrz(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                              @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                              @RequestParam(value = "code", required = false) Integer code,
                              @RequestParam(value = "message", required = false) String message,
                              @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }

        List<Map<String, Object>> errorPushList = kwDfrzPushService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryDfrz",
                "补发东方融资网", createTimeBegin, createTimeEnd, "kw_dfrz_push", errorPushList.size(),
                code + "", message, interfaceCode);
        if (errorPushList != null && errorPushList.size() > 0) {
            for (Map<String, Object> pushMap : errorPushList) {
                String mobile = pushMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    kwDfrzFilterService.sendEastFinance(userInfo, user);
                }
            }
        }
    }

    /**
     * 补发新一贷
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryPACA")
    public void handRetryPACA(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                              @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                              @RequestParam(value = "code", required = false) Integer code,
                              @RequestParam(value = "message", required = false) String message,
                              @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryPACA",
                "补发新一贷", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    userInfoService.sendPcac(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("重新发送由于次数限制导致的失败数据");
                    kwInterfaceRetry.setInterfaceCode("newloan");
                    kwInterfaceRetry.setInterfaceName("平安新一贷注册接口");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(thirdbackMap.get("createDate").toString(), "yyyy-MM-dd HH:mm:ss"));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }
            }
        }
    }


    /**
     * 补发贷上我
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryWithme")
    public void handRetryWithme(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                                @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                                @RequestParam(value = "code", required = false) Integer code,
                                @RequestParam(value = "message", required = false) String message,
                                @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> xxjrPushList = kwXxjrPushService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryWithme",
                "补发贷上我", createTimeBegin, createTimeEnd, "kw_xxjr_push", xxjrPushList.size(),
                code + "", message, interfaceCode);
        if (xxjrPushList != null && xxjrPushList.size() > 0) {
            for (Map<String, Object> xxjrPushMap : xxjrPushList) {
                String id = xxjrPushMap.get("id").toString();
                String mobile = xxjrPushMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    kwXxjrFilterService.sendWithMe(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("重新发送由于次数限制导致的失败数据");
                    kwInterfaceRetry.setInterfaceCode("xxjr");
                    kwInterfaceRetry.setInterfaceName("贷上我接口");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(xxjrPushMap.get("createDate").toString(), "yyyy-MM-dd HH:mm:ss"));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }
            }
        }
    }


    /**
     * 补发新贝
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryXbb")
    public void handRetryXbb(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                             @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                             @RequestParam(value = "code", required = false) Integer code,
                             @RequestParam(value = "message", required = false) String message,
                             @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryXbb",
                "补发新贝", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            logger.info("失败数据共:{}条", thirdbackList.size());
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    kwXbbFilterService.sendXbb(userInfo, user);
                    //接口补录
                    KwInterfaceRetry kwInterfaceRetry = new KwInterfaceRetry();
                    kwInterfaceRetry.setOriginId(id);
                    kwInterfaceRetry.setReason("重新发送由于渠道量已满的失败数据");
                    kwInterfaceRetry.setInterfaceCode("xbb");
                    kwInterfaceRetry.setInterfaceName("新贝保险");
                    kwInterfaceRetry.setOriginDate(DateUtils.strToDate(thirdbackMap.get("createDate").toString(), "yyyy-MM-dd HH:mm:ss"));
                    kwInterfaceRetry.setMobile(mobile);
                    kwInterfaceRetryService.insert(kwInterfaceRetry);
                }
            }
        }
    }


    /**
     * 补发中腾信官方
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryCTC")
    public void handRetryCTC(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                             @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                             @RequestParam(value = "code", required = false) Integer code,
                             @RequestParam(value = "message", required = false) String message,
                             @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryCTC",
                "补发中腾信官方", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            logger.info("失败数据共:{}条", thirdbackList.size());
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    userInfoService.sendChinatopcredit(userInfo, user);
                }
            }
        }
    }

    /**
     * 补发助贷
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryZhudai")
    public void handRetryZhudai(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                                @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                                @RequestParam(value = "code", required = false) Integer code,
                                @RequestParam(value = "message", required = false) String message,
                                @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> zhudaiPushList = kwZhudaiPushService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryZhudai",
                "补发助贷", createTimeBegin, createTimeEnd, "kw_zhudai_push", zhudaiPushList.size(),
                code + "", message, interfaceCode);
        if (zhudaiPushList != null && zhudaiPushList.size() > 0) {
            logger.info("失败数据共:{}条", zhudaiPushList.size());
            for (Map<String, Object> zhudaiPushMap : zhudaiPushList) {
                String mobile = zhudaiPushMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existZhudaiPush = kwZhudaiPushService.getList(params);
                    //已有成功发送记录
                    if (existZhudaiPush != null && existZhudaiPush.size() > 0) {
                        continue;
                    }
                    //获取上一天时间
                    long timemillis = DateUtils.getYesRanTime();
                    userInfo.setCreateDate(new Date(timemillis));
                    kwZhudaiFilterService.sendZhuDai(userInfo, user);
                }
            }
        }
    }


    /**
     * 补发各种接口
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @param message
     * @param interfaceCode
     * @throws Exception
     */
    @RequestMapping("/handRetryAll")
    public void handRetryAll(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                             @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd,
                             @RequestParam(value = "code", required = false) Integer code,
                             @RequestParam(value = "message", required = false) String message,
                             @RequestParam(value = "interfaceCode", required = false) String interfaceCode) throws Exception {
        //查找符合条件的接口发送记录
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        if (StringUtils.isNotBlank(interfaceCode)) {
            map.put("interfaceCode", interfaceCode);
        }
        if (code != null) {
            map.put("code", code);
        }
        if (StringUtils.isNotBlank(message)) {
            map.put("message", message);
        }
        List<Map<String, Object>> thirdbackList = kwThirdbackService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handRetryAll",
                "补发各种接口", createTimeBegin, createTimeEnd, "kw_thirdback", thirdbackList.size(),
                code + "", message, interfaceCode);
        if (thirdbackList != null && thirdbackList.size() > 0) {
            logger.info("失败数据共:{}条", thirdbackList.size());
            for (Map<String, Object> thirdbackMap : thirdbackList) {
                String id = thirdbackMap.get("id").toString();
                String mobile = thirdbackMap.get("mobile").toString();
                //查找用户信息
                User user = userService.getByMobile(mobile);
                UserInfo userInfo = userInfoService.getByUserId(user.getId());
                //组装信息
                //组装接口数据
                if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                    if ("eastfinance-isregister".equals(interfaceCode)) {
                        interfaceCode = "eastfinance-register";
                    }
                    //校验是否有发送成功的记录
                    Map<String, Object> params = new HashMap<>();
                    params.put("interfaceCode", interfaceCode);
                    params.put("mobile", mobile);
                    params.put("code", 1);
                    List<Map<String, Object>> existThirdback = kwThirdbackService.getList(params);
                    //已有成功发送记录
                    if (existThirdback != null && existThirdback.size() > 0) {
                        continue;
                    }
                    if ("ycdph".equals(interfaceCode)) {
                        kwYcdFilterService.sendYCDPH(userInfo, user);
                    } else if ("dfrz".equals(interfaceCode)) {
                        kwDfrzFilterService.sendEastFinance(userInfo, user);
                    }
                }
            }
        }
    }


    //由出生日期获得年龄
    public int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;
    }

}
